package com.app.acitivty;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TableRow;
import android.widget.TextView;

import com.app.activity.R;
import com.app.components.TopMenu;
import com.app.utils.ActivityTools;

public class InnScheduledActivity extends Activity implements OnClickListener {

	private Button btnSearch;

	private TableRow tbInnType;

	private final int MENU1 = 1;
	private final int MENU2 = 2;
	private final int MENU3 = 3;

	private TableRow tbStartDate;
	private TableRow tbEndDate;
	private TextView tvStartDate;
	private TextView tvEndDate;
	private TextView tvPriceRange;
	private TableRow tbPriceRange;

	private TextView tvInnType;

	private final int START_DATE_DIALOG = 1;
	private final int END_DATE_DIALOG = 0;

	private void initView() {
		btnSearch = (Button) findViewById(R.id.btnSearch);
		tbInnType = (TableRow) findViewById(R.id.tbInnType);
		tbStartDate = (TableRow) findViewById(R.id.tbStartDate);
		tbEndDate = (TableRow) findViewById(R.id.tbEndDate);
		tvInnType = (TextView) findViewById(R.id.tvInnType);
		tvStartDate = (TextView) findViewById(R.id.tvStartDate);
		tvEndDate = (TextView) findViewById(R.id.tvEndDate);
		tvPriceRange=(TextView) findViewById(R.id.tvPriceRange);
		tbPriceRange=(TableRow) findViewById(R.id.tbPriceRange);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.inn_scheduled);
		initView();
		btnSearch.setOnClickListener(this);
		TopMenu topmenu = new TopMenu(this, "返回", new OnClickListener() {

			@Override
			public void onClick(View v) {
				InnScheduledActivity.this.finish();
			}
		}, R.drawable.top_button_unselect, R.drawable.top_button_selected,
				"客栈预定");
		topmenu.onCreate();

		tbStartDate.setOnClickListener(this);
		tbEndDate.setOnClickListener(this);

		final DialogInterface.OnClickListener onselect = new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case 0:
					tvInnType.setText("星级酒店");
					break;
				case 1:
					tvInnType.setText("连锁酒店");
					break;
				case 2:
					tvInnType.setText("民居客栈");
					break;

				}
			}

		};
		tbInnType.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				String[] choices = { "星级酒店", "连锁酒店", "民居客栈" };
				AlertDialog dialog = new AlertDialog.Builder(
						InnScheduledActivity.this).setTitle("酒店类型")
						.setItems(choices, onselect).create();
				dialog.show();
			}
		});
		
		final DialogInterface.OnClickListener onPriceselect = new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case 0:
					tvPriceRange.setText("500以下");
					break;
				case 1:
					tvPriceRange.setText("500~1000");
					break;
				case 2:
					tvPriceRange.setText("1000以上");
					break;

				}
			}

		};
		tbPriceRange.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				String[] choices = { "500以下", "500~1000", "1000以上" };
				AlertDialog dialog = new AlertDialog.Builder(
						InnScheduledActivity.this).setTitle("价格范围")
						.setItems(choices, onPriceselect).create();
				dialog.show();
			}
		});

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.btnSearch:
			ActivityTools.gotoActivity(this, InnListActivity.class);
			break;
		case R.id.tbStartDate:
			showDialog(START_DATE_DIALOG);
			break;
		case R.id.tbEndDate:
			showDialog(END_DATE_DIALOG);
			break;

		default:
			break;
		}

	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU1:
			tvInnType.setText("星级酒店");
			break;
		case MENU2:
			tvInnType.setText("连锁酒店");
			break;
		case MENU3:
			tvInnType.setText("民居客栈");
			break;
		}

		return true;
	}

 

	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		switch (id) {
		case START_DATE_DIALOG:
			Calendar c = Calendar.getInstance();
			dialog = new DatePickerDialog(this,
					new DatePickerDialog.OnDateSetListener() {
						public void onDateSet(DatePicker dp, int year,
								int month, int dayOfMonth) {
							tvStartDate.setText(year + "-" + (month + 1) + "-"
									+ dayOfMonth);
						}
					}, c.get(Calendar.YEAR), c.get(Calendar.MONTH),
					c.get(Calendar.DAY_OF_MONTH));
			break;
		case END_DATE_DIALOG:
			Calendar c1 = Calendar.getInstance();
			dialog = new DatePickerDialog(this,
					new DatePickerDialog.OnDateSetListener() {
						public void onDateSet(DatePicker dp, int year,
								int month, int dayOfMonth) {
							tvEndDate.setText(year + "-" + (month + 1) + "-"
									+ dayOfMonth);
						}
					}, c1.get(Calendar.YEAR), c1.get(Calendar.MONTH),
					c1.get(Calendar.DAY_OF_MONTH));
			break;
		}
		return dialog;

	}

}
